package com.cykj.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author 王宇
 * @CreateTime: 2025-07-25  16:57
 * @BelongsProject: jd_seckill
 * @BelongsPackage: com.cykj.config
 * @description redis配置类
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 进行序列化
        // 针对key进行序列化
        template.setKeySerializer(new StringRedisSerializer());

        // 针对value进行序列化
        FastJsonRedisSerializer<Object> objectFastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        template.setValueSerializer(objectFastJsonRedisSerializer);

        // 针对hash进行序列化
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(objectFastJsonRedisSerializer);

        // 设置支持事务处理
        template.setEnableTransactionSupport(true);

        return template;
    }
}
